﻿#include <stdio.h>
#include "AvProto.h"
#include "log.h"

void _onServiceEvent(unsigned int ev, void* data)
{
	switch (ev)
	{
	case _ukProtoRespond | __MsgConnect: {
		C_Connect* _Connect = (C_Connect*)(data);
		time_t ct = time(NULL);
		if (abs(ct - _Connect->TimeVal_TvSec) > 3) {
			//set system time;;;
		}
	}break;

	case _ukProtoGet | __MsgSystemVersion: {
		C_SystemVersion* _SystemVersion = (C_SystemVersion*)(data);
		sprintf(_SystemVersion->AppVersionString, "V4.5.6.9");
	}break;

	case _ukProtoGet | __MsgNetCommProfile: {
		C_NetCommProfile* _NetCommProfile = (C_NetCommProfile*)(data);
		_NetCommProfile->NetGetType = NetCommT_Wireless;
		sprintf(_NetCommProfile->IpAddr, "%s", "192.168.1.101");
		sprintf(_NetCommProfile->SubMask, "%s", "255.255.255.0");
		sprintf(_NetCommProfile->GateWay, "%s", "192.168.1.1");
		sprintf(_NetCommProfile->Mac, "%s", "00:11:22:33:44:56");
		sprintf(_NetCommProfile->DnsI, "%s", "8.8.8.8");
		sprintf(_NetCommProfile->DnsII, "%s", "9.9.9.9");
	}break;

	default:
		break;

	}
}

static void _onDevLog(LogItem* logItem)
{
	AvProtoPushDevLog(logItem);
}
int main(int argc, char* argv[])
{
	//6f4b84f8-59c7-4a66-afa4-b5b6291272f8
	//int _ret = AvProtoServiceStart("10001", NULL, _onServiceEvent);
	_logInit();
	int _ret = AvProtoServiceStart(NULL, "5623080000000078", _onServiceEvent);
	if (_ret < 0) {
		_logDebugErr("AvProtoServiceStart failed _ret = %d exit app\n", _ret);
		return -1;
	}

	LogProfile* _Profile;
	_Profile = _logGetProfile();
	_Profile->bPrintFileNameLine = 1;
	_Profile->bPrintLevel = TLogWarn;
	_Profile->LogProc = _onDevLog;
	_logSetProfile(_Profile);

	_logMsg("this is _logMsg Test = %ld\n", time(NULL));
	_logWarn("this is _logWarn Test = %ld\n", time(NULL));
	_logErr("this is _logErr Test = %ld\n", time(NULL));
	_logHint("this is _logHint Test = %ld\n", time(NULL));


	//以下是用来测试协议的，工程中不应该出现以下代码，按上面_logMsg 写就可以-----
	LogItem _iteam;
	int i = 0;
	struct timeval tv;
	while (1) {
		AppRunMSec(_iteam.UpTime);
		AvGetTimeOfDay(&tv, NULL);
		_iteam.FileLine = 10000 + i;
		sprintf(_iteam.FileName, "Sample_%06d.c", i);
		_iteam.Timeval_tvSec = tv.tv_sec;
		_iteam.Timeval_tvUsec = tv.tv_usec;
		_iteam.Type = 1;
		sprintf(_iteam.Msg, "Sample Msg_%03d\n", i);

		AvProtoPushDevLog(&_iteam);
		AvMSleep(1000);
	}
	return 0;
}
